﻿
function ml_lookup(listTagID, aArray, selectedvalue){

  //this.formName = formName;
  //this.listName = listName;
  this.oList = document.getElementById(listTagID);
  this.init  = init;
  this.populate   = populate;
  this.clearList  = clearList;
  this.add_selected_node = add_selected_node;
  this.add_child_node    = add_child_node;
  this.add_option_to_list = add_option_to_list;
  this.nodes = new Object();
  this.init(aArray);


  var theObject = this;
  this.oList.onchange = function(){
     var  curr_selected = this.options[this.selectedIndex].value;
     theObject.populate(curr_selected);
   };

  this.populate(selectedvalue);

}

// only the first level and the selected list will be selected
function populate(selectedNode){
	this.clearList();

  var selected_index = -1;
	if (selectedNode == ''){ // Nothing has selected
		var aSlibing = this.nodes['c'+0];
		for (var i = 0; i < aSlibing.length; i++){
			var child_id = aSlibing[i];
      var newOptionIndex = this.add_option_to_list(0, child_id);
    }
    this.oList.selectedIndex = -1; // do not know which category is selected
    return;
	}


	var aPath = new Array();

	var node_id = selectedNode;
  var topid = this.nodes['n'+node_id].topid;
  while ( topid  > 0){
    aPath.unshift(topid);
  	node_id = topid;
  	topid = this.nodes['n'+node_id].topid;
  }


  var first_node = aPath.shift();
  var last_node  = selectedNode;


  // add all first level child
	var aChild = this.nodes['c'+0];


  for (var i = 0; i < aChild.length; i++){
  	var child_id = aChild[i];
  	var curr_index = this.add_option_to_list(0, child_id);

    if (child_id == selectedNode){
 				selected_index = this.add_child_node(0, child_id);
 				selected_index = curr_index;
 			}

 		if (child_id == first_node){


 			var j = 0; // also means depth
 			for ( j = 0; j < aPath.length; j++){
 				var mid_node = aPath[j];
 				this.add_option_to_list(j+1, mid_node);
    	}
    	selected_index = this.add_selected_node(this.oList, j, last_node);



    }
  } // for  aChild.length


  if (this.oList.options.length == 0){
    this.oList.options[this.oList.options.length] = new Option("[none available]",0);
  }



  this.oList.selectedIndex = selected_index;
} // end function populate

function get_indent_char(depth){
	var indent = '　';
	var temp ='';
	for (var i = 0; i < depth; i++){
		temp += indent;
	}
	return temp;
} // end function get_indent_char

function add_selected_node(oList, depth, node_id){
  var selected_index = -1;
  depth = depth+1;

	var parent_id = this.nodes['n'+node_id].topid;
	var aSlibing  = this.nodes['c'+parent_id];

	for (var i = 0; i < aSlibing.length; i++){
	  var n = aSlibing[i];
	  var curr_index = this.add_option_to_list(depth, n);

	  if (n == node_id){
	  	selected_index = curr_index;
	  	this.add_child_node(depth, node_id);
	  }
  }

  return selected_index;
} // end function add_selected_node


function add_option_to_list(depth, node_id){
	var indent = get_indent_char(depth);
	var currIndex = this.oList.options.length;
	this.oList.options[currIndex] = new Option(indent+this.nodes['n'+node_id].name, this.nodes['n'+node_id].id);
  var indent_padding = 20*depth;
  //this.oList.options[currIndex].setAttribute("style","color:red;text-indent:"+indent_padding+"px;");

	return currIndex;
} // end function add_option_to_list


function add_child_node(depth, node_id){
	if (this.nodes.hasOwnProperty('c'+node_id)){
		var aChild = this.nodes['c'+node_id]; // selected node child
		for (var j = 0; j < aChild.length; j++){
			var child_id = aChild[j];
			this.add_option_to_list(depth+1, child_id);
		}
	}

}  // end function add_child_node

function clearList(){

 for (var i = this.oList.options.length; i >= 0; i-=1){
  this.oList.options[i] = null;
 }
 this.oList.selectedIndex = -1;
} // end function clearList

// covert from source array to internal tree structure
function init(aArray){

	for (var i = 0; i < aArray.length; i= i + 3){
    var node = new Object();
    node.id    = aArray[i];
    node.topid = aArray[i+1];
    node.name  = aArray[i+2];

    this.nodes['n'+aArray[i]] = node;


    // build child list
    var child;
    if (!this.nodes.hasOwnProperty('c'+aArray[i+1])){
    	child = [];
    } else {
    	child = this.nodes['c'+aArray[i+1]];
    }

    child.push(node.id);
    this.nodes['c'+aArray[i+1]] = child;

  }
} // end function init